<?php
/**
 * Created by PhpStorm.
 * User: Zhuojianhe
 * Date: 2018/1/23
 * Time: 14:00
 */

namespace App\Services\Cart;

/**
 *优惠活动
 */
class ActivityService
{
    
    public function __construct($activity)
    {
        $this->act_id=$activity->act_id;
        $this->act_name=$activity->act_name;
        $this->act_promote_goods_type=$activity->act_promote_goods_type;
        $this->act_promote_goodsArray=explode(",",$activity->act_promote_ext);
        $this->act_range=$activity->act_range;
        $this->act_range_ext=explode(",",$activity->act_range_ext);
        $this->act_type=$activity->act_type;
        $this->act_type_ext=$activity->act_type_ext;
        $this->user_rankArray=explode(",",$activity->user_rank);
        $this->min_amount=$activity->min_amount;
        $this->max_amount=$activity->max_amount;
        $this->start_time=$activity->start_time;
        $this->end_time=$activity->end_time;
        $this->gift_goodsArray=unserialize($activity->gift);
        $this->user_rank=$activity->user_rank;
        $this->sort_order=$activity->sort_order;
        $this->is_include_subcategory=$activity->is_include_subcategory;
        $this->limited_number=$activity->limited_number;
        $this->limited_discount=$activity->limited_discount;
        
    }

    /**
     * @var void 有多少商品参与了该活动
     */
    private $ParticipateNum=0;

    /**
     * @var void 有多少商品可参与该活动
     */
    private $ParticipateCanNum=0;



    /**
     * @var void 参与本活动的商品
     */
    private $GoodsArray;
    private $sort_order;
    private $touch_img;
    private $is_include_subcategory;
    private $limited_number;
    private $limited_discount;
    private $need_add_price=0;
    private $user_rankArray=array();

    /**
     * @var void 活动ID
     */
    private $act_id;

    /**
     * @var void 活动名称
     */
    private $act_name;

    /**
     * @var void 可参与本活动的会员等级
     */
    private $user_rank;

    /**
     * @var void 最小金额
     */
    private $min_amount;

    /**
     * @var void 最大金额
     */
    private $max_amount;

    /**
     * @var void 活动开始时间
     */
    private $start_time;
    
    /**
     * @var void 活动结束时间
     */
    private $end_time;

    /**
     * @var void 活动类型
     */
    private $act_type;

    /**
     * @var void
     */
    private $act_type_ext;

    /**
     * @var void 增品或换购列表
     */
    private $gift_goodsArray;

    /**
     * @var void
     */
    private $act_promote_goods_type;

    /**
     * @var void
     */
    private $act_promote_goodsArray;

    /**
     * @var void
     */
    private $act_range;

    /**
     * @var void
     */
    private $act_range_ext;
    
    private $discount;

    private $isSatisfies=0;

    /**
     * @param void $GoodsArray  //添加参与的商品
     */
    public function addGoods($Goods)
    {
        $goods_code=$Goods->getGoodsId().'_'.$Goods->getGiving();
        $this->GoodsArray[$goods_code] = $Goods;
    }

    /**
     * 删除参与的商品
     */
    public function deleteGoods($Goods)
    {
        $goods_code=$Goods->getGoodsId().'_'.$Goods->getGiving();
        unset($this->GoodsArray[$goods_code]);
    }

    /**
     * 增加参与商品数量+1
     */
    public function inc(){
        $this->ParticipateNum+=1;
    }

    /**
     * 减少参与商品数量-1
     */
    public function red(){
        $this->ParticipateNum-=1;
    }

    /**
     * 增加参与商品数量+1
     */
    public function canInc(){
        $this->ParticipateCanNum+=1;
    }

    /**
     * 减少参与商品数量-1
     */
    public function canRed(){
        $this->ParticipateCanNum-=1;
    }

    /**
     * @return void
     */
    public function getParticipateCanNum()
    {
        return $this->ParticipateCanNum;
    }

    /**
     * @return void
     */
    public function getParticipateNum()
    {
        return $this->ParticipateNum;
    }

    public function resetIsSatisfies(){
        $Gift=array(
            "remove"=>array(),
            "add"=>array(),
            "setNum"=>array()
        );

        $totalPrice=0;
        $totalNumber=0;
        $totalGoodsNumber=0;
        $totalGiftPrice=0;
        $GiftGoods=null;
        foreach($this->GoodsArray as $goods){
            if(!in_array($goods->getGiving(),[1,2])){
                $totalPrice+=$goods->getCurrentNumber()*$goods->getCurrentPrice();
                $totalGoodsNumber+=$goods->getCurrentNumber();
                $totalNumber+=1;
            }else{
                $GiftGoods=$goods;
            }
        }

        foreach($this->gift_goodsArray as $gift){
            $totalGiftPrice+=$gift['price'];
        }

        if($this->checkIsSatisfies() && $this->isSatisfies==0){
            //如果活动由不满足达到满足条件
            if($this->act_type==0){
                //单一赠品
                if(count($this->gift_goodsArray)==1 and $totalGiftPrice==0){
                    $Gift['add']['goods_id']=$this->gift_goodsArray[0]['goods_id'];
                    $Gift['add']['goods_stock']=$this->gift_goodsArray[0]['goods_stock'];
                }
                $this->discount=0;
            }else if($this->act_type==1){
                //满减
                $this->discount=$this->act_type_ext;
            }else if($this->act_type==2){
                //折扣
                $this->discount=$totalPrice*(1-($this->act_type_ext/100));
            }
            $this->isSatisfies=1;

        }else if(!$this->checkIsSatisfies() && $this->isSatisfies==1){
            //如果活动由满足降为不满足条件
            //*清赠品和换购商品
            if($this->act_type==0){
                foreach($this->GoodsArray as $goods){
                    if(in_array($goods->giving,[1,2])){
                        $Gift["remove"][]=$goods->goods_id.'_'.$goods->giving;
                    }
                }
            }

            $this->discount=0;
            $this->isSatisfies=0;
        }else if($this->checkIsSatisfies() && $this->isSatisfies==1){
            //处理买一赠一商品--增减商品数量
            if($this->act_type==0){
                //单一赠品
                if(count($this->gift_goodsArray)==1 and $totalGiftPrice==0){
                    if($totalNumber==1){
                        //*买一赠一 --增减商品数量
                        if($totalGoodsNumber>$this->act_type_ext){
                            $GiftGoods->setCurrentNumber($this->act_type_ext);
                            
                        }else{
                            $GiftGoods->setCurrentNumber($totalGoodsNumber);
                        }
                        $Gift["setNum"]["rec_id"]=$GiftGoods->getRecId();
                        $Gift["setNum"]["num"]=$GiftGoods->getCurrentNumber();
                        
                    }
                }
            }

        }
        return $Gift;
    }

    /**
     * 判断该活动是否满足条件
     */
    public function checkIsSatisfies(){
        //判断时间是否有效
        if($this->end_time<gm_time()){
            return false;
        }
        //判断下限金额
        $totalPrice=0;
        foreach($this->GoodsArray as $goods){
            //总计选中的商品
            if($goods->getIsCheck()==1 && $goods->getGoodsStock()<$goods->getCurrentNumber()){
                $totalPrice+=$goods->getCurrentPrice()*$goods->getCurrentNumber();
            }

        }
        if($totalPrice<$this->min_amount){
            $this->need_add_price=$this->min_amount-$totalPrice;
            return false;
        }
        $this->need_add_price=0;
        //判断上限金额
        /*if($totalPrice>$this->max_amount){
            return false;
        }*/
        return true;
    }

    /**
     * @role 获取活动标签
     * @author Jianhe
     */
    public function getActTag(){
        return 1;
    }

    /**
     * @role 格式化赠品、换购商品-输出信息
     * @author Jianhe
     * @return int
     */
    public function getGiftFormat(){
        return 1;
    }

    /**
     * @role 格式化活动-输出信息
     * @author Jianhe
     * @return array
     */
    public function getActivityFormat(){
        $activityFormat=array();
        $activityFormat['act_id']=$this->act_id;
        $activityFormat['act_name']=$this->act_name;
        $activityFormat['act_promote_ext']=implode(",",$this->act_promote_goodsArray);
        $activityFormat['act_promote_goods_type']=$this->act_promote_goods_type;
        $activityFormat['act_range']=$this->act_range;
        $activityFormat['act_range_ext']=$this->act_range_ext;
        $activityFormat['act_tag']=$this->getActTag();
        $activityFormat['act_type']=$this->act_type;
        $activityFormat['act_type_ext']=$this->act_type_ext;
        $activityFormat['discount']=$this->discount;
        $activityFormat['start_time']=gmdate("Y-m-d H:i:s",$this->start_time);
        $activityFormat['end_time']=gmdate("Y-m-d H:i:s",$this->end_time);
        $activityFormat['formated_max_amount']=price_format($this->max_amount);
        $activityFormat['formated_min_amount']=price_format($this->min_amount);
        $activityFormat['gift']=$this->getGiftFormat();
        $activityFormat['is_include_subcategory']=$this->is_include_subcategory;
        $activityFormat['limited_discount']=$this->limited_discount;
        $activityFormat['limited_number']=$this->limited_number;
        $activityFormat['max_amount']=$this->max_amount;
        $activityFormat['min_amount']=$this->min_amount;
        $activityFormat['quota']=[];
        $activityFormat['sort_order']=$this->sort_order;
        $activityFormat['touch_img']=$this->touch_img;
        $activityFormat['user_rank']=$this->user_rank;

        return $activityFormat;

    }

    private function getParticipateGoodsFormat(){
        $goods_list=array();
        $goods_total_price=0;
        $goods_total_vip_price=0;
        foreach($this->GoodsArray as $goods){
            $goods_list[]=$goods->getGoodsFormat();
            $goods_total_price+=$goods->getCurrentNumber()*$goods->getCurrentPrice();
            $goods_total_vip_price+=$goods->getCurrentNumber()*$goods->getVipPrice();
        }
        $result['goods_list']=$goods_list;
        $result['goods_total_price']=$goods_total_price;
        $result['goods_total_vip_price']=$goods_total_vip_price;
        return $result;
    }

    /**
     * @role 格式化活动_商品-输出信息
     * @author Jianhe
     */
    public function getActivityWithGoodsFormat(){
        $ParticipateGoods=$this->getParticipateGoodsFormat();
        $activityGoodsFormat=array();
        $activityGoodsFormat['can_redemption']=$this->isSatisfies;
        $activityGoodsFormat['discount']=$this->discount;
        $activityGoodsFormat['goods_list']=$ParticipateGoods['goods_list'];
        $activityGoodsFormat['goods_total_price']=$ParticipateGoods['goods_total_price'];
        $activityGoodsFormat['goods_total_vip_price']=$ParticipateGoods['goods_total_vip_price'];
        $activityGoodsFormat['info']=$this->getActivityFormat();
        $activityGoodsFormat['need_add_price']=get_price($this->need_add_price);
        $activityGoodsFormat['total_amount']=$ParticipateGoods['goods_total_price']-$this->discount;
        $activityGoodsFormat['total_vip_amount']=$ParticipateGoods['goods_total_vip_price']-$this->discount;

        return $activityGoodsFormat;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getGoodsArray()
    {
        return $this->GoodsArray;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getActId()
    {
        return $this->act_id;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getActName()
    {
        return $this->act_name;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getUserRankArray()
    {
        return $this->user_rankArray;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getMinAmount()
    {
        return $this->min_amount;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getMaxAmount()
    {
        return $this->max_amount;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getStartTime()
    {
        return $this->start_time;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getEndTime()
    {
        return $this->end_time;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getActType()
    {
        return $this->act_type;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getActTypeExt()
    {
        return $this->act_type_ext;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getGiftGoodsArray()
    {
        return $this->gift_goodsArray;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getActPromoteGoodsType()
    {
        return $this->act_promote_goods_type;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getActPromoteGoodsArray()
    {
        return $this->act_promote_goodsArray;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getActRange()
    {
        return $this->act_range;
    }

    /**
     * @author Jianhe
     * @return void
     */
    public function getActRangeExt()
    {
        return $this->act_range_ext;
    }

    /**
     * @author Jianhe
     * @param void $gift_goodsArray
     */
    public function setGiftGoodsArray($gift_goodsArray)
    {
        $this->gift_goodsArray = $gift_goodsArray;
    }
    
    
    
    
    
    
    
    
    
    






    
    

    
}
